package com.sunday.player.util.lazyimage;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.R;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.provider.BaseColumns;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.AlbumColumns;

import com.sunday.player.domain.ImageInfo;


public class getImage {
    private HashMap<String, SoftReference<Drawable>> cache = new HashMap<String, SoftReference<Drawable>>();    
    ExecutorService executorService;
    private Context mContext;
    Handler handler=new Handler();//handler to display images in UI thread
    public getImage(Context mContext) {
	executorService=Executors.newFixedThreadPool(5);
	this.mContext = mContext;
    }
    
    public void addGetimage(ImageInfo info){
	
        SoftReference<Drawable> bitmap=cache.get(info.AlbumId);
        
        if(bitmap != null && bitmap.get() != null){
           
           info.iv.setImageDrawable(bitmap.get());
          
        } else
        {
           
            queuePhoto(info);
            info.iv.setImageResource(R.drawable.ic_menu_rotate);
        }
    }

    private void queuePhoto(ImageInfo info) {
	executorService.execute(new PhotosLoader(info));
    }
    class BitmapDisplayer implements Runnable
    {
        Bitmap bitmap;
        ImageInfo item;
        int index = R.drawable.ic_menu_rotate;
        public BitmapDisplayer(ImageInfo item , Bitmap bitmap){
        	this.item = item;	
        	this.bitmap = bitmap;
        }
        public BitmapDisplayer(ImageInfo item , int bitmap){
        	this.item = item;	
        	this.index = bitmap;
        }
        public void run()
        {
            
        	// 2013-03-11 구글 보고서 보고 수정함.
        	if((item.AlbumId != null && item.iv != null)    &&     ((String)item.iv.getTag()).equals(item.AlbumId)){
        	    	
        		if(bitmap != null){
	        		item.iv.setImageBitmap(bitmap);
	        	}else{
	        		item.iv.setImageResource(index);
	        	}
        	
        	}
        }
    }
    class PhotosLoader implements Runnable {
	ImageInfo photoToLoad;
        PhotosLoader(ImageInfo photoToLoad){
            this.photoToLoad=photoToLoad;
        }
        
        @Override
        public void run() {
            try{
                
                if(LoadImageToMP3(photoToLoad)){
                    
                }
                
                
            }catch(Throwable th){
                th.printStackTrace();
            }
        }

	
	private boolean LoadImageToMP3(ImageInfo item){
		
		
		try {
			String imagepath = null;
			Cursor c = mContext.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
					new String [] {
					BaseColumns._ID,
					AlbumColumns.ALBUM_ART
			}, "_id="+item.AlbumId, null, null);

			if(c.moveToFirst()) {
				imagepath = c.getString(1);
			}
			c.close();
		
			
			
			
			BitmapFactory.Options opt = new BitmapFactory.Options();
			if( item.Type == item.Size_Thum){
				opt.inSampleSize = 10;
			}else{
				opt.inSampleSize = 1;
					
			}
			opt.inPurgeable = true;
			opt.inDither = true;
			Bitmap bitmap=BitmapFactory.decodeFile(imagepath, opt);
			setImage(item,bitmap);
			
			
			
	        
	      
		}catch (OutOfMemoryError e){
			e.printStackTrace();
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return false;
	}

	private void setImage(ImageInfo item, Bitmap bitmap) {
	    if(bitmap != null){
		SoftReference<Drawable> drawRef = new SoftReference<Drawable>(new BitmapDrawable(bitmap));;
		    cache.put(item.AlbumId, drawRef);
		    
	    }
	    
	  
	    BitmapDisplayer bd=new BitmapDisplayer(item,bitmap);
            handler.post(bd);
	    
	}
	
	
	
    }

}
